﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Model;

using System.IO;

using System.Data.OleDb;
using System.Data;

using System.Text;
namespace zjbks
{
    public partial class AdminAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            Model.UserInfoEntity user = new UserInfoEntity();
            user.ID = id.Text.Trim();
            user.Pwd = pwd.Text.Trim();
            user.Type = type.SelectedValue;
            BLL.BUserInfo bUser = new BLL.BUserInfo();
            Model.UserInfoEntity user1 = new UserInfoEntity();
            user1 = bUser.GetUserInfo(user.ID);
            if(user1==null)
            {
                if(bUser.AddUserInfo(user)>0)
                {
                    //成功
                    ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('添加成功！');</script>");
                }
                else
                {
                    //失败
                    ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('添加失败！');</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('用户存在！');</script>");
            }
        }
        /// <summary>
        /// 导入Excel文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            // 指定保存位置
            string savepath = System.Web.HttpContext.Current.Server.MapPath("./files/");
            if (FileUpload1.HasFile)
            {
                string filename = FileUpload1.FileName;
                savepath += filename;
                FileUpload1.SaveAs(savepath);
                try
                {
                    ExcelToMSSql.DataTranfUser(savepath);
                    ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('导入成功')</script>");
                }
                catch (Exception ex)
                {
                    ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('导入失败')</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('请选择文件')</script>");
            }
        }
    }
}